package com.example.bankend.service.method;



import com.example.bankend.mapper.CardMapper;
import com.example.bankend.model.BankCard;
import com.example.bankend.service.impl.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;

    @Override
    public int insert(BankCard card) {
        return cardMapper.insert(card);
    }

    @Override
    public int deleteByPrimaryKey(String cardID) {
        return cardMapper.deleteByPrimaryKey(cardID);
    }

    @Override
    public int updateByPrimaryKey(BankCard card) {
        return cardMapper.updateByPrimaryKey(card);
    }
    //修改密码
    @Override
    public int updatePassByCardID(BankCard bankCard) {
        return cardMapper.updatePassByCardID(bankCard);
    }
    @Override
    public BankCard selectByPrimaryKey(String cardID) {
        return cardMapper.selectByPrimaryKey(cardID);
    }
    //挂失
    @Override
    public int reportLoss(String cardID){
        BankCard card = cardMapper.selectByPrimaryKey(cardID);
        if (card != null) {
            card.setIsReportLoss("是"); // 将状态置为”是“表示挂失
            return cardMapper.updateByPrimaryKey(card);
        }
        return 0;
    }
    @Override
    public int dereportLoss(String cardID){
        BankCard card = cardMapper.selectByPrimaryKey(cardID);
        if (card != null) {
            card.setIsReportLoss("否");
            return cardMapper.updateByPrimaryKey(card);
        }
        return 0;
    }
    @Override
    public List<BankCard> selectAll() {
        return cardMapper.selectAll();
    }
    @Override
    public List<BankCard> getCardsByUsername(String username) {
        return cardMapper.getCardsByUsername(username);
    }


    //某一个用户总经济
    @Override
    public Double getTotalBalanceByUsername(String username) {
        List<BankCard> cards = cardMapper.getCardsByUsername(username);
        if (cards != null && !cards.isEmpty()) {
            return cards.stream().mapToDouble(BankCard::getBalance).sum();
        }
        return null;
    }

    @Override
    public Double getTotalBalance() {
        return cardMapper.getTotalBalance();
    }
    @Override
    public int getTotalCards() {
        return cardMapper.getTotalCards();
    }
}
